package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.Role;
import com.zch.house.service.RoleService;
import com.zch.house.service.impl.RoleServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class RoleServlet
 */
@WebServlet("/RoleServlet2")
public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    RoleService rs = new RoleServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("grbi".equals(method)){
			getRoleById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ro_name = request.getParameter("name");
		String ro_desc = request.getParameter("desc");
		String createTimeStr = request.getParameter("name");
		Date ro_create_time = DateFormat.to_Date2(createTimeStr);
		Role role = new Role(ro_name, ro_desc, ro_create_time);
		boolean flag = rs.addRole(role);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='RoleServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='RoleServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = rs.deleteRole(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='RoleServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='RoleServlet2?method=select'</script>");
		}
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String ro_name = request.getParameter("name");
		String ro_desc = request.getParameter("desc");
		String createTimeStr = request.getParameter("name");
		Date ro_create_time = DateFormat.to_Date2(createTimeStr);
		Role role = new Role(id, ro_name, ro_desc, ro_create_time);
		boolean flag = rs.updateRole(role);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 修改成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='RoleServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='RoleServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String pageIndexStr = request.getParameter("pageIndex");
		if(pageIndexStr==null||pageIndexStr==""){
			pageIndexStr = "1";
		}
		int pageIndex = Integer.parseInt(pageIndexStr);
		List<Role> list = rs.getAllRoleByPage(name, pageIndex);
		int totalPages = rs.getTotalCount(name);
		request.setAttribute("name", name);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("list", list);
		request.setAttribute("totalPages", totalPages);
		request.getRequestDispatcher("view/role/list.jsp").forward(request, response);
		
	}

	private void getRoleById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		Role role = rs.getRoleById(id);
		request.setAttribute("role", role);
		request.getRequestDispatcher("view/role/edit.jsp").forward(request, response);		
	}

}
